﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;

namespace MobileUI
{
    public partial class Profile : System.Web.UI.Page
    {
        SqlConnection conn;
        SqlCommand cmd;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UID"] == null) Response.Redirect("~/Default.aspx");
            conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["MobileConnectionString"].ConnectionString);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (!txtPass.Text.Equals(txtRePass.Text))
            {
                Response.Write("<script>alert('Re type password not match!');window.location = '../Profile.aspx';</script>");
                return;
            }
            try
            {
                conn.Open();
                cmd = new SqlCommand()
                {
                    Connection = conn,
                    CommandType = CommandType.Text,
                    CommandText = "update Customer set Password = @Password Where Id = @Id"
                };
                cmd.Parameters.Add(new SqlParameter("@Id", Session["CID"]));
                cmd.Parameters.Add(new SqlParameter("@Password", txtPass.Text.Trim()));
                var result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    conn.Close();
                    Response.Write("<script>alert('Your password has been change!'); window.location= '../Default.aspx'</script>");
                }

            }
            catch (Exception)
            {
                Response.Write("<script>alert('Some error has ocurred!');window.location = '../Profile.aspx';</script>");
            }
        }
    }
}